#include "Model.h"
#include "Vertex.h"
#include "NFG.h"
#include "string.h"

Model::Model(void)
{
	m_vboId = 0;
	m_iboId = 0;
	m_numOfIndices = 0;
	m_id[0] = '\0';
}

void Model::Init(const char *file, const char *id)
{
	// init id
	strcpy(m_id, id);

	// load Model	
	int vertexNum;
	Vertex *verticesData;
	unsigned short *indices;
	LoadNFG(file, vertexNum, verticesData, m_numOfIndices, indices);

	//buffer object
	glGenBuffers(1, &m_vboId);
	glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
	glBufferData(GL_ARRAY_BUFFER, vertexNum * sizeof(Vertex), verticesData, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	delete[] verticesData;

	glGenBuffers(1, &m_iboId);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iboId);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_numOfIndices * sizeof(unsigned short), indices, GL_STATIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	delete[] indices;
}

GLuint Model::GetVboId()
{
	return m_vboId;
}

GLuint Model::GetIboId()
{
	return m_iboId;
}

GLuint Model::GetNumOfIndices()
{
	return m_numOfIndices;
}

const char* Model::GetId()
{
	return m_id;
}

Model::~Model(void)
{
	if (m_vboId)
	{
		glDeleteBuffers(1, &m_vboId);
	}

	if (m_iboId)
	{
		glDeleteBuffers(1, &m_iboId);
	}
}
